package com.bckj.fastboot.iot.data.mysql.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bckj.fastboot.iot.common.dto.query.IotAlarmDataQuery;
import com.bckj.fastboot.iot.common.dto.response.IotAlarmDataResponse;
import com.bckj.fastboot.iot.data.mysql.entity.IotAlarmData;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * IotAlarmDataMapper
 *
 * @author wfc
 * @date 2025/9/30 11:23
 **/
@Mapper
public interface IotAlarmDataMapper extends BaseMapper<IotAlarmData> {

    @SelectProvider(type = IotAlarmDataProvider.class, method = "findById")
    IotAlarmDataResponse findById(Long id);

    @SelectProvider(type = IotAlarmDataProvider.class, method = "findLatestOne")
    IotAlarmDataResponse findLatestOne(IotAlarmDataQuery query);

    @SelectProvider(type = IotAlarmDataProvider.class, method = "findList")
    List<IotAlarmDataResponse> findList(IotAlarmDataQuery query);

    class IotAlarmDataProvider {

        public String findById(Long id) {
            return "select * from iot_alarm_data where id = #{id}";
        }

        public String findLatestOne(IotAlarmDataQuery query) {
            SQL sql = new SQL();
            sql.SELECT("*");
            sql.FROM("iot_alarm_data");
            sql.WHERE("dev_code = #{devCode}");
            sql.WHERE("alarm_code = #{alarmCode}");
            if (query.getAlarmStatus() != null) {
                sql.WHERE("alarm_status = #{alarmStatus}");
            }
            sql.ORDER_BY("id desc");
            sql.LIMIT(1);
            return sql.toString();
        }

        public String findList(IotAlarmDataQuery query) {
            SQL sql = new SQL();
            sql.SELECT("*");
            sql.FROM("iot_alarm_data");
            listWhere(sql, query);
            return sql.toString();
        }

        private void listWhere(SQL sql, IotAlarmDataQuery query) {

        }
    }
}
